package com.dhecp.project.flowable.controller;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.annotation.Log;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.project.flowable.domain.ActHistoricActivity;
import com.dhecp.project.flowable.domain.ActHistoricInstance;
import com.dhecp.project.flowable.domain.ActRuntimeInstance;
import com.dhecp.project.flowable.service.ActProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ActProcessInstController
 * @Description TODO
 * @Author alula
 * @Date 2019-12-03 11:32
 */
@Controller
@RequestMapping("/flowable/process")
public class ActProcessInstController extends BaseController {

    private String prefix = "flowable/process";

    @Autowired
    private ActProcessService actProcessService;

    //运行中的流程的数据
    @RequestMapping("/runProcessList")
    @ResponseBody
    public TableDataInfo runProcessList(@RequestParam Map<String, Object> params) {
    	Integer toolBarWidth = 300;
        params.put("toolBarWidth", toolBarWidth);
        startPage();
        List<ActRuntimeInstance> list = actProcessService.queryActRuntimeProcess(params);
        return getDataTable(list);
    }

    //完成中的流程的数据
    @RequestMapping("/finishProcessList")
    @ResponseBody
    public TableDataInfo finishProcessList(@RequestParam Map<String, Object> params) {
    	Integer toolBarWidth = 300;
        params.put("toolBarWidth", toolBarWidth);
        startPage();
        List<ActHistoricInstance> list = actProcessService.queryActFinishProcess(params);
        return getDataTable(list);
    }

    @Log(title = "流程实例更新状态", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updateProcessInstanceStatus(@RequestParam String instanceId, @RequestParam Integer status){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = actProcessService.updateProcessInstanceStatus(instanceId, status);
            return ajaxResult;
        }
        catch (Exception e) {
            return AjaxResult.error(this.getExceptionMessage(e));
        }
    }

    /**
     * 加载流程进度页面
     */
    @GetMapping("/processSpeed/{instanceId}")
    public String processSpeed(@PathVariable("instanceId") String instanceId, ModelMap model)
    {
        model.put("instanceId", instanceId);
        List<ActHistoricActivity> historicActivityList = actProcessService.queryActHistoricActivityByProcInstId(instanceId);
        model.put("historicActivityList", historicActivityList);
        
        System.out.println(JSON.toJSONString(historicActivityList));
        return prefix + "/processspeed";
    }

    /**
     * 获取流程图
     */
    @RequestMapping(value = "/getProcessImg/{instanceId}", method = RequestMethod.GET)
    public void getProcessImg(@PathVariable String instanceId,
                                HttpServletResponse response) throws Exception {
        InputStream inputStream = actProcessService.getActivitiProccessImage(instanceId);
        try {
            if (inputStream != null) {
                // 设置页面不缓存
                response.setHeader("Pragma", "No-cache");
                response.setHeader("Cache-Control", "no-cache");
                response.setDateHeader("Expires", 0);
                response.setContentType("image/png");
                OutputStream os = response.getOutputStream();
                int bytesRead = 0;
                byte[] buffer = new byte[1024];
                while ((bytesRead = inputStream.read(buffer, 0, 1024)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
                os.close();
                inputStream.close();
            }
        } catch (IOException e) {
            throw new Exception("读取流程图片失败!");
        }
    }
}
